package ControlLayer;

import java.util.Date;
import java.util.LinkedList;

import DBLayer.DBRoom;
import DBLayer.IFDBRoom;
import ModelLayer.KeyWord;
import ModelLayer.Room;
import ModelLayer.RoomType;

public class RoomCtr {

	public RoomCtr() {

	}

	public int insertRoom(String name, int rtId, String status) {
		Room room = new Room();
		room.setName(name);
		room.setRoomType(new RoomType(rtId));
		room.setStatus(KeyWord.valueOf(status));
		IFDBRoom dbRoom = new DBRoom();
		return dbRoom.insertRoom(room);
	}

	public int updateRoom(int roomId, String name, Date endDate, int rtId,
			String status) {
		Room room = new Room();
		room.setName(name);
		room.setRoomType(new RoomType(rtId));
		room.setStatus(KeyWord.valueOf(status));
		room.setEndDate(endDate);
		room.setId(roomId);
		IFDBRoom dbRoom = new DBRoom();
		return dbRoom.updateRoom(room);
	}

	public int deleteRoom(int roomId) {
		IFDBRoom dbRoom = new DBRoom();
		return dbRoom.deleteRoom(roomId);
	}

	// return all the rooms from one roomtype
	public LinkedList<Room> getRoomsForRT(int rtId) {
		IFDBRoom dbRoom = new DBRoom();
		return (dbRoom.getAllRoomForRT(rtId, true));
	}

	// return the number of available rooms for one room type
	public int getAvailableRoomsNrForRT(int rtId) {
		LinkedList<Room> rooms = getRoomsForRT(rtId);
		// TODO introduct a bug, when room is occupied for some period, the
		// available rooms will decrease
		// int nr = 0;
		// for (Room room : rooms) {
		// if (room.getStatus() == KeyWord.Available)
		// nr++;
		// }
		return rooms.size();
	}

	public Room getRoom(int rId) {
		IFDBRoom dbRoom = new DBRoom();
		return dbRoom.getRoom(rId, true);
	}
}
